<template>
  <div class="process-start-list">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>选择流程</span>
        </div>
      </template>

      <el-table :data="definitions" v-loading="loading">
        <el-table-column prop="name" label="流程名称" />
        <el-table-column prop="key" label="流程标识" />
        <el-table-column prop="version" label="版本" width="100" />
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button 
              type="primary" 
              size="small" 
              @click="handleStart(row)"
            >
              发起流程
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'

const router = useRouter()
const loading = ref(false)
const definitions = ref([])

const loadDefinitions = async () => {
  loading.value = true
  try {
    const response = await request.get('/process-definitions')
    definitions.value = response.data
  } catch (error) {
    console.error('加载流程定义失败:', error)
    ElMessage.error('加载流程定义失败')
  } finally {
    loading.value = false
  }
}

const handleStart = (row) => {
  if (!row.key) {
    ElMessage.error('流程标识不能为空')
    return
  }
  router.push({
    name: 'ProcessStart',
    params: { processKey: row.key }
  })
}

onMounted(() => {
  loadDefinitions()
})
</script>

<style scoped>
.process-start-list {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style> 